package com.haochang.chunkvod.controller.activity.hostess;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshBase;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshListView;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

/**
 * 预收/代付---预收列表
 *     @author Yangjie
 *     className AdvancesReceivedListActivity
 *     created at  2016/8/23  18:08
 */
public class AdvancesReceivedListActivity extends BaseActivity
{

	private TitleView titleView;
	private PullToRefreshListView listView;
	private HostessData hostessData;
	private QuickAdapter<HostessInfo> adapter;
	private List<HostessInfo> hostessInfoList = new ArrayList<HostessInfo>();

	@Override
	public void receiveParam()
	{
	}

	@Override
	public void initData()
	{
		hostessData = new HostessData(AdvancesReceivedListActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_advances_received_list);
		titleView = (TitleView) findViewById(R.id.titleView);
		listView = (PullToRefreshListView) findViewById(R.id.listView);
		titleView.setTitle(R.string.advances_receives_list).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("回到首页");
				list.add("修改密码");
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(AdvancesReceivedListActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							backToHome();
							break;
						case 1:
							goToPasswordModify();
							break;
						case 2:
							loginOut();
							break;

						default:
							break;
						}
					}
				});
			}
		});
		requestAdvancesReceivedList();
	}

	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{//显示弹出框
			HostessInfo item = (HostessInfo) v.getTag();
			if (item != null)
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append("流水号:").append(item.getName()).append("\n");
				stringBuilder.append("预收时间:").append(item.getStatementTime()).append("\n");
				stringBuilder.append("类型:").append(item.getType()).append("\n");
				stringBuilder.append("金额:").append(item.getType()).append("\n");
				stringBuilder.append("备注:").append(item.getType()).append("\n");
				stringBuilder.append("已挂入房间:").append(item.getType()).append("\n");
				new VodDialog.Builder(AdvancesReceivedListActivity.this).autoDismiss(true).dialogEnum(VodDialog.DialogEnum.ONLY_TOAST)
						.contentName(stringBuilder).setContentGravity(Gravity.LEFT).build().showWithWindowParams(0);
			} else
			{
				ToastUtils.showText(AdvancesReceivedListActivity.this, "挂入房间信息获取异常!");
			}
		}
	};

	@Override
	public void initViewsData()
	{
		super.initViewsData();
		adapter = new QuickAdapter<HostessInfo>(this, R.layout.item_advances_received, hostessInfoList)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, HostessInfo item)
			{
				//				helper.setText(R.id.tv_amount, item.getMoney());
				//				helper.setText(R.id.tv_room, item.getRoomNum());
				helper.setText(R.id.tv_time, "我是预收时间" + helper.getPosition());
				helper.getView(R.id.tv_click).setTag(item);
				helper.getView(R.id.tv_click).setOnClickListener(onBaseClickListener);
			}
		};
		listView.setAdapter(adapter);
		listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<BaseListView>()
		{
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{
				requestAdvancesReceivedList();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{

			}
		});
	}

	/**
	 * 获取预收list
	 */
	@SuppressWarnings("unchecked")
	private void requestAdvancesReceivedList()
	{
		hostessData.requestAdvancesReceivedList("", new HostessData.ResultResponseList()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				listView.onRefreshComplete();
				if (!CollectionUtils.isEmpty(list))
				{
					hostessInfoList.clear();
					adapter.replaceAll(list);
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				listView.onRefreshComplete();
				ToastUtils.showText(AdvancesReceivedListActivity.this, TextUtils.isEmpty(errMsg) ? "获取预收列表失败!" : errMsg);
			}
		});
	}
}
